package org.sbbs.security.model;

/*
 org.sbbs.app.security.model.UrlSecurityResource
 *
 */
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Formula;
import org.sbbs.base.model.BaseObject;

@Entity
@Table(name = "t_url_resource")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class UrlResource extends BaseObject {
	/**
	 *
	 */
	private static final long serialVersionUID = -4497886079128610490L;
	private Long id;
	private String nameSpace;
	private String name;
	private String url;
	private List<Permission> permissions = new ArrayList<Permission>();


	private Boolean isMenuUrl;

	// private Menu menu;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(length = 20, nullable = false)
	public String getNameSpace() {
		return nameSpace;
	}

	public void setNameSpace(String nameSapce) {
		this.nameSpace = nameSapce;
	}

	@Column(length = 50, nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length = 200, nullable = false, unique = true)
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	@Formula(value=" (SELECT COUNT(u.id)=1 from t_url_resource u INNER JOIN t_menu m on u.id=m.urlResource_id where u.id = id) ")
	public Boolean getIsMenuUrl() {
		return isMenuUrl;
	}

	public void setIsMenuUrl(Boolean isMenuUrl) {
		System.out.println("this url id is: " + id + ", it's is menu value is " + isMenuUrl);
		this.isMenuUrl = isMenuUrl;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "t_url_permission", joinColumns = { @JoinColumn(name = "url_id") }, inverseJoinColumns = @JoinColumn(name = "perm_id"))
	public List<Permission> getPermissions() {
		return permissions;
	}

	/**
	 * 设置 permissions 的值
	 *
	 * @param permissions
	 */
	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	@Override
	public String toString() {
		return "UrlSecurityResource [nameSapce=" + nameSpace + ", name=" + name + ", url=" + url + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((nameSpace == null) ? 0 : nameSpace.hashCode());
		result = prime * result + ((url == null) ? 0 : url.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UrlResource other = (UrlResource) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (nameSpace == null) {
			if (other.nameSpace != null)
				return false;
		} else if (!nameSpace.equals(other.nameSpace))
			return false;
		if (url == null) {
			if (other.url != null)
				return false;
		} else if (!url.equals(other.url))
			return false;
		return true;
	}

}
